package com.study.talk.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.ui.adapter.base.BaseListAdapter
import com.study.talk.databinding.UsersItemBinding
import com.study.talk.data.model.UserInfo
import com.study.talk.utils.extras.toChat
import com.study.talk.utils.extras.toUserDetails

class UsersAdapter(private val target: Int) : BaseListAdapter<UserInfo, UsersAdapter.ViewHolder>() {

    companion object {
        const val DETAIL = 0
        const val CHAT = 1
    }

    class ViewHolder(val context: Context, val binding: UsersItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context, DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.users_item, parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.user = getItem(position)
        holder.binding.root.setOnClickListener {
            holder.context.apply {
                when (target) {
                    DETAIL -> {
                        toUserDetails(getItem(position).id)
                    }

                    CHAT -> {
                        toChat(getItem(position).id)
                    }
                }
            }
        }
    }
}
